/*

	Copyright (c) 2011, The Chinese University of Hong Kong

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.

*/

#ifndef IDOCK_PRECALCULATION_HPP
#define IDOCK_PRECALCULATION_HPP

#include "common.hpp"
#include <boost/function.hpp>

namespace idock
{
	using boost::function;

	class precalculation
	{
	public:
		static const size_t Num_Segments = 1000000; // 1M

		precalculation(const function<fl (fl)> f, const fl lower_bound, const fl upper_bound, const size_t num_segments = Num_Segments) : lower_bound(lower_bound), upper_bound(upper_bound), num_segments(num_segments), length((upper_bound - lower_bound) / num_segments), length_inverse(1 / length)
		{
			BOOST_ASSERT(lower_bound <= upper_bound);
			for (size_t i = 0; i < num_segments; ++i)
			{
				prec[i] = f(lower_bound + length * i);
			}
		}

		fl evaluate(const fl x) const
		{
			BOOST_ASSERT(x >= lower_bound);
			BOOST_ASSERT(x <  upper_bound);
			return prec[static_cast<size_t>((x - lower_bound) * length_inverse)];
		}

	private:
		const fl lower_bound;
		const fl upper_bound;
		const size_t num_segments;
		const fl length;
		const fl length_inverse;

		vector<fl> prec;
	};

	class sin_wrapper
	{
	public:
		fl operator()(const fl radius) const
		{
			return std::sin(radius);
		}
	};
}

#endif
